/**
 * Project Name:cats-web
 * File Name:RoleController.java
 * Package Name:com.catsic.web.controllers
 * Date:2014年4月2日下午5:04:49
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */

package com.catsic.web.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.controller.BaseController;
import com.catsic.core.UserInfo;
import com.catsic.core.conditon.RoleCondition;
import com.catsic.core.entity.Role;
import com.catsic.core.form.MenuForm;
import com.catsic.core.form.OrganForm;
import com.catsic.core.form.RoleForm;
import com.catsic.core.menu.service.MenuService;
import com.catsic.core.organ.service.OrganService;
import com.catsic.core.resource.service.ResourceService;
import com.catsic.core.role.service.RoleService;
/**
 * ClassName: RoleController 
 * Description: 角色控制器 
 * date: 2014年4月2日 下午5:04:49
 * author: yangyd
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private ResourceService resourceService;

	@Autowired
	private OrganService organService;

	/**
	 * query:查询
	 * @author yangyd
	 * @param condition
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> query(RoleCondition condition, Pageable pageable) {
		Page<RoleForm> page = roleService.findAll(condition, pageable);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, page);
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * create:创建
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> create(RoleForm form) {
		form = roleService.add(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT,form);
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * update:更新
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update(RoleForm form) {
		form = roleService.update(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT,form);
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * delete:删除
	 * @author yangyd
	 * @param role 角色
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String,Object> delete(String id) {
		roleService.delete(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * detail:详情
	 * @author yangyd
	 * @param id 
	 * @return
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Map<String,Object> detail(String id) {
		RoleForm form = roleService.findRoleById(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * bind:绑定
	 * @author yangyd
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/bind")
	@ResponseBody
	public Map<String,Object> bind(Role role) {
		role = roleService.bind(role);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * bindOrgan:绑定
	 * @author yangyd
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/bindOrgan")
	@ResponseBody
	public Map<String,Object> bindOrgan(Role role) {
		role = roleService.bindOrgan(role);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * findMenusById:通过角色ID获取对应的菜单.
	 * @author yangyudi
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findMenusById", method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> findMenusById(String id){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, roleService.findMenusById(id));
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * findByOrganId:通过机构ID，获取机构绑定的角色
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findByOrganId")
	@ResponseBody
	public Map<String,Object> findByOrganId(String organId) {
		List<RoleForm> listForm = roleService.findByOrganId(organId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, listForm);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
		
	/**
	 * getBindRole:获取绑定的角色
	 * @author yangyd
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/findBindRole")
	@ResponseBody
	public Map<String,Object> findBindRole(String userId) {
		List<RoleForm> listForm = roleService.getBindRole(userId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, listForm);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * findByUserId:通过用户ID，获取用户绑定的角色
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findByUserId")
	@ResponseBody
	public Map<String,Object> findByUserId(String userId) {
		List<RoleForm> listForm = roleService.findByUserId(userId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, listForm);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * findOrganById:通过角色ID，获取角色绑定的机构
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findOrganById")
	@ResponseBody
	public Map<String,Object> findOrganById(String id) {
		List<OrganForm> listForm = roleService.findOrganById(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, listForm);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * findResourcesById:通过角色ID获取对应的资源.
	 * @author yangyudi
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findResourcesById", method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> findResourcesById(String id){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, roleService.findResourcesById(id));
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * 通过用户ID获取用户菜单
	 * @return
	 */
	@RequestMapping(value="/findUserMenuByUserId")
	@ResponseBody
	public Map<String,Object> findUserMenuByUserId(){
		UserInfo userInfo = (UserInfo) getCurrentUser();
		List<MenuForm> menuForm = menuService.findUserMenuFormByUserId(userInfo.getUserId());
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		map.put(CONTENT, menuForm);
		return map;
	}
}
